
function [DMES,intervEqUnique,EqPayoffs] = createDMES(intervEq,V_R,V_US,V_UK,V_France,V_Russia,V_China,YY,specification)

% specification (column index vector):
% 1-6: rebels',US,UK,FR,RU,CH ordinal payoffs
% 7-12: rebels',US,UK,FR,RU,CH binary payoffs (best eq.)
% 13: on-path exp. number of interveners
% 14: subgame exp. number of interveners
% 15: pure strategy eq.
% 16: war
% 17: Pareto dominated

YY=1*(YY>0);

[NS,N] = size(intervEq);
DMES = cell(NS,N);
if nargout==3
    EqPayoffs = DMES;
end

for n=1:N
    for b=1:NS
        if ~isempty(intervEq{b,n})
            intervEq{b,n} = unique(intervEq{b,n}','rows','stable')';
            EQ = intervEq{b,n};
            neq = size(EQ,2);
            spne = EQ(1,:)>=V_R(1,b,n);
            EQ(1,spne)=0;
            EQ(:,~spne)=repmat([1;zeros(size(EQ,1)-1,1)],1,sum(~spne));
            ExpInterv = EQ'*sum(YY(:,2:end),2);
            ExpIntervSG= intervEq{b,n}(2:end,:)'*sum(YY(2:end,2:end),2);
            A = [intervEq{b,n}(1,:)',intervEq{b,n}(2:end,:)'*[V_US(2:end,b,n),V_UK(2:end,b,n),V_France(2:end,b,n),V_Russia(2:end,b,n),V_China(2:end,b,n)]];
            A(A(:,1)<V_R(1,b,n),:) = repmat([V_R(1,b,n),V_US(1,b,n),V_UK(1,b,n),V_France(1,b,n),V_Russia(1,b,n),V_China(1,b,n)],sum(A(:,1)<V_R(1,b,n)),1);
            if neq>1
                Pareto = zeros(neq);
                for i=1:neq
                    for j=i+1:neq
                        if sum(A(i,:)==A(j,:))~=numel(A(j,:))
                            Pareto(i,j)=1*(sum(A(i,:)>=A(j,:))==numel(A(j,:)))-1*(sum(A(i,:)<=A(j,:))==numel(A(j,:)));
                        end
                    end
                end
                Pareto = triu(Pareto) - triu(Pareto)';
                DMES{b,n} = [tiedrank(A)/neq, 1*(A==max(A)), ExpInterv, ExpIntervSG, 1*(sum(EQ==1)'==1), 1*(EQ(1,:)'==0), 1*(sum(Pareto<0,2)>0)];
            else
                DMES{b,n} = [ones(1,12), ExpInterv, ExpIntervSG, 1*(sum(EQ==1)'==1), 1*(EQ(1,:)'==0), 0];
            end
            DMES{b,n} = DMES{b,n}(:,specification);
            if nargout==3
                EqPayoffs{b,n} = A;
            end
        end
    end
end

intervEqUnique = intervEq;
